package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.*;
import com.gec.model.vo.ITripCityQueryVo;
import com.gec.model.vo.ITripHotelVo;
import com.gec.system.common.Result;
import com.gec.system.service.ITripCityService;
import com.gec.system.service.ITripHotelRoomService;
import com.gec.system.service.ITripHotelService;
import com.gec.system.service.ITripImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 酒店表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/system/itrHotel")
@Api(tags = "酒店管理")
@CrossOrigin
public class ITripHotelController {
    @Autowired
    private ITripHotelService iTripHotelService;

    @Autowired
    private ITripHotelRoomService iTripHotelRoomService;

    @Autowired
    private ITripImageService iTripImageService;


    @ApiOperation("根据id去移除一个角色")
    // 测试删除
    @DeleteMapping("/removeHotel/{id}")
    public Result removeHotel(@PathVariable Long id) {
        boolean b = this.iTripHotelService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

//    uni-app

    @ApiOperation("uni-app 获取全部酒店")
    @GetMapping("/findAll")
    public Result findHotelByPageQuery() {

        //2.调用方法
       List<ITripHotel> list = this.iTripHotelService.selectPage();
        //3.返回
        return Result.ok(list);
    }

    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findHotelByPageQuery(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       ITripHotelVo iTripHotelQueryVo) {
        //1.创建分页对象
        IPage<ITripHotel> p1 = new Page<>(page, limit);
        //2.调用方法
        p1 = this.iTripHotelService.selectPage(p1, iTripHotelQueryVo);
        //3.返回
        return Result.ok(p1);
    }
    // 添加

    @PostMapping("/addHotel")
    public Result addHotel(@RequestBody ITripHotel iTripHotel) {
        //        添加编号唯一性判断

        ITripHotel it = iTripHotelService.selectByName(iTripHotel.getHotelName());
        if (it != null) {
            return Result.fail("name重复");
        }

        ITripImage image = new ITripImage();
        image.setImage(iTripHotel.getImage());
        image.setType("0");
        image.setUrl(iTripHotel.getUrl());
        image.setTargetId(iTripHotel.getId());
        iTripImageService.saveOrUpdate(image);

        boolean res = this.iTripHotelService.updateById(iTripHotel);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    // 修改

    @GetMapping("/findHotelById/{id}")
    public Result findHotelById(@PathVariable Long id) {
        ITripHotel iTripHotel = this.iTripHotelService.getById(id);

        return Result.ok(iTripHotel);
    }


    // 实现修改
    @PostMapping("/updateHotel")
    public Result updateHotel(@RequestBody ITripHotel iTripHotel) {
        boolean b = this.iTripHotelService.updateById(iTripHotel);
        ITripImage image = new ITripImage();
        image.setImage(iTripHotel.getImage());
        image.setType("0");
        image.setUrl(iTripHotel.getUrl());
        image.setTargetId(iTripHotel.getId());
        Long id = iTripImageService.getId(0,iTripHotel.getId());
        image.setId(id);
        iTripImageService.saveOrUpdate(image);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 批量删除
    @DeleteMapping("/removeHotelByIds")
    public Result removeHotelByIds(@RequestBody List<Long> ids) {
        boolean b = this.iTripHotelService.removeByIds(ids);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

//    对标uni-app

    //    选择酒店  显示所有的房间信息
    @ApiOperation("获取全部酒店列表")

    @GetMapping("/chooseHotel/{id}")
    public Result chooseHotel(@PathVariable Long id) {
        List<ITripHotel> list =  iTripHotelRoomService.getByHotelId(id);
        return Result.ok(list);
    }
}

